import React from 'react';
import {connect} from 'react-redux';
import {Toast} from 'antd-mobile';
import {AuthRoute,LocalParam} from "../../../assets/js/utils";
import Css from '../../../assets/css/user/order/index.css';
import request from "../../../assets/js/utils/request";
import HeadNavComponent from "../../../components/headNav";
class addReview extends React.Component{
    constructor(props){
        super(props);
        AuthRoute(props);
        this.state={
            review:[],
            content:'',

        }
        this.uGid=LocalParam(props.location.search).search.gid?LocalParam(props.location.search).search.gid:'';
        this.ordernum=LocalParam(props.location.search).search.ordernum?LocalParam(props.location.search).search.ordernum:'';
        this.isSubmit=true
    }
    componentDidMount(){
        this.getDataReview()
    }
    //点击选着评分
    selecStore(index,index2){
        let temScores=this.state.review;
        for (let i in temScores[index].scores){
            temScores[index].scores[i].checked=false;
            for(let j=0; j<=index2; j++){
                temScores[index].scores[j].checked=true;
            }
        }
        this.setState({review:temScores})

    }
    //获取评分数据
    getDataReview(){
        let sUrl=window.base.config.baseUrl+'home/reviews/service?token='+window.base.config.token;
        request(sUrl,'get').then(res=>{
            if (res.code===200){
                let temScores=res.data;
                for (let i in temScores) {
                    temScores[i].scores=[
                        {
                            checked:false,
                            score:1
                        },
                        {
                            checked:false,
                            score:2
                        },
                        {
                            checked:false,
                            score:3
                        },
                        {
                            checked:false,
                            score:4
                        },
                        {
                            checked:false,
                            score:5
                        }
                    ]
                }

                this.setState({review:temScores})
            }
        })
    }
    //提交评分
    submitSave(){
        let temScores=this.state.review,
            score=[],
            scores=0,
            temRsdata=[],
            isChecked=false;
        for (let i=0; i< temScores.length;i++){
            isChecked=false;
            for (let j=0; j<temScores[i].scores.length;j++){
                if(temScores[i].scores[j].checked){
                    isChecked=true;
                    break;
                }
            }
            if(!isChecked){
                Toast.info('请选择'+temScores[i].title,1);
                return;
            }
        }
        if (this.state.content.match(/^\s*$/)) {
            Toast.info('请输入评价',1);
            return;
        }
        //组装评价数据
        for (let i=0; i< temScores.length;i++){
            for (let j=0; j<temScores[i].scores.length;j++){
                if(temScores[i].scores[j].checked){
                    score.push(temScores[i].scores[j].score);
                }
            }
            scores=score[score.length-1];
            temRsdata.push({
                gid:this.uGid,
                myid:this.props.state.redLogin.uid,
                rsid:this.state.review[i].rsid,
                score:scores
            })
        }
        if(this.isSubmit){
            this.isSubmit=false;
            let sUrl=window.base.config.baseUrl+'home/reviews/add?token='+window.base.config.token,
                data={
                    uid:this.props.state.redLogin.uid,
                    gid:this.uGid,
                    content:this.state.content,
                    ordernum:this.ordernum,
                    rsdata:JSON.stringify(temRsdata)
                };
            request(sUrl,'post',data).then(res=>{
                if (res.code===200){
                    Toast.info(res.data,1)
                }else {
                    Toast.info(res.data,1)
                }
            })
        }

    }
    //输入评价
    testareaVule(e){
        this.setState({content:e.target.value})
    }
    componentWillUnmount(){
        this.setState = (state, callback) => {
            return;
        };
    }
    render(){
        return(
            <div className={Css['order_main_review']}>
                <HeadNavComponent title='评价'></HeadNavComponent>
                {
                    this.state.review.length>0?
                        this.state.review.map((item,index)=>{
                            return(
                                <div className={Css['review_grop']} key={index}>
                                    <div className={Css['review_row']}>{item.title}</div>
                                    <div className={Css['review_row']}>
                                        {
                                            item.scores.length>0?
                                                item.scores.map((item2,index2)=>{
                                                    return(
                                                        <i onClick={this.selecStore.bind(this,index,index2)} className={item2.checked?Css['active']+' pentagram':' pentagram'} key={index2}></i>
                                                    )
                                                })
                                                :''
                                        }
                                    </div>
                                </div>
                            )
                        })
                        :''
                }
                <div className={Css['review_grop']}>
                    <textarea placeholder='请输入您的评价' onChange={this.testareaVule.bind(this)} value={this.state.content}></textarea>
                </div>
                <div className={Css['review_btn']} onClick={this.submitSave.bind(this)}>提交</div>
            </div>
        )
    }
}
export default connect((state)=>{
    return{
        state:state
    }
})(addReview)